# coding=utf8
import ConfigParser
import time

import RPi.GPIO as GPIO

from config import Config

'''
红外测距开关
'''


class InfraredSensor:

    def __init__(self, gpio):
        self.infraredPin = int(gpio)
        GPIO.setmode(GPIO.BCM)
        GPIO.setwarnings(False)
        GPIO.setup(self.infraredPin, GPIO.IN)  # 红外传感器

    def checkObstacle(self):
        '''
        是否有障碍物，如果有返回1，如果没有返回0
        :return: 1/0
        '''

        if GPIO.input(self.infraredPin) == True:
            return 1
        else:
            return 0


if __name__ == '__main__':
    # 调用简单demo
    try:
        while True:
            infraredSensor = InfraredSensor(Config().read("Sys_Pins", "infraredSensor_pin"))
            if infraredSensor.checkObstacle() == 1:  # 有障碍物警告
                print('warning.....')
                time.sleep(0.5)
            else:  # 无障碍物提示
                print('ok......')
                time.sleep(0.5)
    except KeyboardInterrupt:
        GPIO.cleanup()
